package apy.codecase.business.view.fragment;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import apy.codecase.R;
import apy.codecase.business.presenter.DiaryImpl;
import apy.codecase.business.view.activity.AddDiaryActivity;
import apy.codecase.greendao.DiaryDaoUtils;
import apy.codecase.model.DiaryDaoModel;
import apy.codecase.rv.BaseListFragment;
import apy.codecase.rv.BaseViewHolder;
import apy.codecase.utils.ASPUtils;
import apy.codecase.utils.AToast;
import apy.codecase.utils.Constants;
import butterknife.BindView;

/**
 * Created by Administrator on 2017/10/26.
 * 日记
 */

public class DiaryFragment extends BaseListFragment<List<DiaryDaoModel>, DiaryDaoModel> {

    private static final int REQUEST_DATA = 100;

    @BindView(R.id.diary_del_all)
    Button diaryDelAll;
    @BindView(R.id.diary_cancel)
    Button diaryCancel;
    @BindView(R.id.diary_fab_add)
    FloatingActionButton diaryFabAdd;
    @BindView(R.id.diary_bottom_ll)
    LinearLayout bottomLL;

    private int updatePos;

    /**
     * 判断CheckBox是否显示
     */
    private boolean isCheckVis;
    // 存储勾选框状态的map集合
    private Map<Integer, Boolean> checkMap = new HashMap<>();
    private DiaryDaoUtils diaryDaoUtils;

    @Override
    protected BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
        return new DiaryViewHolder(View.inflate(parent.getContext(), R.layout.item_diary_fragment, null));
    }

    private boolean isDel;
    @Override
    public void initData() {
        super.initData();
        presenter = new DiaryImpl(getActivity(), this);
        diaryDaoUtils = new DiaryDaoUtils(getActivity());
        presenter.loadData(diaryDaoUtils);
        recycler.enableLoadMore(false);
        recycler.enablePullToRefresh(false);

        diaryFabAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddDiaryActivity.class);
                startActivityForResult(intent, REQUEST_DATA);
            }
        });

        diaryCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isCheckVis = false;
                adapter.notifyDataSetChanged();
            }
        });

        diaryDelAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = checkMap.size() - 1; i >= 0; i--) {
                    if (checkMap.get(i)) {
                        isDel = true;
                        DiaryDaoModel diaryDaoModel = mDataList.get(i);
                        diaryDaoUtils.deleteDiaryDaoModel(diaryDaoModel);
                        mDataList.remove(i);
                    }
                }
                if(!isDel){
                    AToast.showTextToast("没有选中任何条目，删个毛啊");
                    return;
                }
                bottomLL.setVisibility(View.GONE);
                isCheckVis = false;
                adapter.notifyDataSetChanged();
                checkMap.clear();
                isDel = false;
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (resultCode == REQUEST_DATA) {
                DiaryDaoModel diaryDaoModel = (DiaryDaoModel) data.getSerializableExtra("data");
                mDataList.add(0, diaryDaoModel);
                adapter.notifyDataSetChanged();
            } else if (resultCode == 200) {
                if (updatePos!=0){
                    DiaryDaoModel diaryDaoModel = new DiaryDaoModel();
                    diaryDaoModel.setDate(ASPUtils.getString(Constants.DIARY_DATE));
                    diaryDaoModel.setTitle(ASPUtils.getString(Constants.DIARY_TITLE));
                    diaryDaoModel.setContent(ASPUtils.getString(Constants.DIARY_CONTENT));
                    diaryDaoModel.setIsDraft(true);
                    mDataList.remove(updatePos);
                    mDataList.add(updatePos,diaryDaoModel);
                    adapter.notifyItemChanged(updatePos);
                    updatePos = 0;
                }else{
                    DiaryDaoModel diaryDaoModel = new DiaryDaoModel();
                    diaryDaoModel.setDate(ASPUtils.getString(Constants.DIARY_DATE));
                    diaryDaoModel.setTitle(ASPUtils.getString(Constants.DIARY_TITLE));
                    diaryDaoModel.setContent(ASPUtils.getString(Constants.DIARY_CONTENT));
                    diaryDaoModel.setIsDraft(true);
                    mDataList.add(0, diaryDaoModel);
//                adapter.notifyItemChanged(0, diaryDaoModel);
                    adapter.notifyDataSetChanged();
                }

            }else if(resultCode == 300){
                DiaryDaoModel diaryDaoModel = (DiaryDaoModel) data.getSerializableExtra("data");
                mDataList.remove(updatePos);
                mDataList.add(updatePos,diaryDaoModel);
                adapter.notifyItemChanged(updatePos);
                updatePos = 0;
            }
        }
    }

    @Override
    public void afterLoadData(List<DiaryDaoModel> t) {
        super.afterLoadData(t);
        if (t != null) {
            Collections.reverse(t);
            mDataList.clear();
            mDataList.addAll(t);
        }
        adapter.notifyDataSetChanged();
        recycler.onRefreshCompleted();
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_diary;
    }

    @Override
    public void onRefresh(int action) {

    }

    @Override
    public void loadNetData() {
        recycler.setRefreshing();
    }

    class DiaryViewHolder extends BaseViewHolder {
        @BindView(R.id.diary_item_iv_circle)
        ImageView diaryItemIvCircle;
        @BindView(R.id.diary_item_date)
        TextView diaryItemDate;
        @BindView(R.id.diary_item_title)
        TextView diaryItemTitle;
        @BindView(R.id.diary_item_content)
        TextView diaryItemContent;
        @BindView(R.id.diary_item_cb)
        CheckBox checkBox;
        @BindView(R.id.diary_item_draft)
        TextView diaryItemDraft;

        public DiaryViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onLongItemClick(View view, int position) {
            isCheckVis = true;
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onBindViewHolder(final int position) {
            final DiaryDaoModel diaryDaoModel = mDataList.get(position);
            if (isCheckVis) {
                checkBox.setVisibility(View.VISIBLE);
                checkBox.setChecked(false);
                bottomLL.setVisibility(View.VISIBLE);
            } else {
                checkBox.setVisibility(View.GONE);
                bottomLL.setVisibility(View.GONE);
            }


            if (diaryDaoModel.getIsDraft()) {
                diaryItemDraft.setVisibility(View.VISIBLE);
            } else {
                diaryItemDraft.setVisibility(View.GONE);
            }
            diaryItemTitle.setText(diaryDaoModel.getTitle());
            diaryItemDate.setText(diaryDaoModel.getDate());
            diaryItemContent.setText("  " + diaryDaoModel.getContent());
            checkMap.put(position, false);
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    checkMap.put(position, isChecked);
                }
            });
        }

        @Override
        public void onItemClick(View view, int position) {
            DiaryFragment.this.updatePos = position;
            Intent intent = new Intent(getActivity(), AddDiaryActivity.class);
            Long id = mDataList.get(position).get_id();
            intent.putExtra("itemId",id);
            startActivityForResult(intent, REQUEST_DATA);
        }
    }
}
